const { app, BrowserWindow } = require('electron');
const path = require('path');
const server = require('./server');

function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      contextIsolation: true,
      nodeIntegration: false,
    }
  });

  // Load the web management interface
  // In development, this will point to the Vue dev server
  // In production, this will load the built index.html file
  mainWindow.loadURL('http://localhost:3000'); 

  // Open the DevTools.
  // mainWindow.webContents.openDevTools();
}

app.whenReady().then(() => {
  // Start the backend server
  server.start();

  createWindow();

  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    server.stop();
    app.quit();
  }
});